package com.chenqq;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.bumptech.glide.load.DecodeFormat;
import com.suntop.carousel.qlmediaplay.R;
import com.suntop.carousel.qlmediaplay.banner.NewFragmentBanner;
import com.suntop.carousel.qlmediaplay.banner.NewViewPagerBanner;
import com.suntop.carousel.qlmediaplay.bean.Option;
import com.suntop.carousel.qlmediaplay.bean.PlayerState;
import com.suntop.carousel.qlmediaplay.field.Field_Banner_Player;
import com.suntop.carousel.qlmediaplay.field.Field_Scale;
import com.suntop.carousel.qlmediaplay.tools.Rxbus;

import java.util.ArrayList;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link ChildFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class ChildFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_Program_id = "program_id";
    private static final String ARG_Program_List_id = "program_list_id";

    // TODO: Rename and change types of parameters
    private int program_id;
    private int program_list_id;
    private View inflate;
    private FrameLayout frameLayout;
    private NewFragmentBanner fragmentBanner;


    public ChildFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @return A new instance of fragment ChildFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static ChildFragment newInstance(int program_list_id, int program_id) {
        ChildFragment fragment = new ChildFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_Program_id, program_id);
        args.putInt(ARG_Program_List_id, program_list_id);

        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            program_id = getArguments().getInt(ARG_Program_id);
            program_list_id = getArguments().getInt(ARG_Program_List_id);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        if (inflate == null) {
            inflate = inflater.inflate(R.layout.fragment_child, container, false);
        }
        return inflate;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
    }

    private void initView() {
        frameLayout = inflate.findViewById(R.id.frameLayout_base);
        fragmentBanner = new NewFragmentBanner(this.getContext());
        frameLayout.removeAllViews();
        fragmentBanner.setId(R.id.viewPager2_main+program_id);
        frameLayout.addView(fragmentBanner, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        ArrayList<Option> urls = new ArrayList<>();
        switch (program_id) {
            case 0:

                Option option2 = new Option();
                option2.setPath("sdcard/2.mp4");
                option2.setDelayTime(5000);
//                option2.setPlayType(1);
                option2.setProgram_list_id(program_list_id);
                option2.setProgram_id(program_id);
                option2.setDecodeFormat(DecodeFormat.PREFER_RGB_565);
                option2.setScaleType(Field_Scale.Scale_Match);
                option2.setLoop(false);
                urls.add(option2);
                break;
            case 1:
                Option option = new Option();
                option.setPath("http://le.sycdn.kuwo.cn/102f3c1062f5bfd27562a446d9b3a819/62849c9c/resource/m3/6/1/2455581671.mp4");
                option.setDelayTime(5000);
                option.setProgram_list_id(program_list_id);
                option.setProgram_id(program_id);
//                option.setPlayType(1);
                option.setDecodeFormat(DecodeFormat.PREFER_RGB_565);
                option.setScaleType(Field_Scale.Scale_Match);
                option.setLoop(false);
                urls.add(option);

                Option option5 = new Option();
                option5.setPath("sdcard/海洋.mov");
                option5.setDelayTime(5000);
//                option5.setPlayType(1);
                option5.setProgram_list_id(program_list_id);
                option5.setProgram_id(program_id);
                option5.setDecodeFormat(DecodeFormat.PREFER_RGB_565);
                option5.setScaleType(Field_Scale.Scale_Match);
                option5.setLoop(false);
                urls.add(option5);

                break;
            case 3:
                Option option1 = new Option();
                option1.setPath("http://win.web.nf03.sycdn.kuwo.cn/bb3a37cdadd2866b7e5e1a46157009b4/6281a530/resource/m1/62/88/3855040819.mp4");
                option1.setDelayTime(5000);
                option1.setDecodeFormat(DecodeFormat.PREFER_RGB_565);
                option1.setScaleType(Field_Scale.Scale_Match);
                option1.setLoop(false);
                urls.add(option1);
                break;
        }
        fragmentBanner.setProgramInfo(program_list_id, program_id, 11223+program_id);
        fragmentBanner.addResource(urls);
        Log.e(" 初始化 ", " ChildFragment initView()=" + program_id);
    }

    @Override
    public void onStart() {
        Log.e(" 初始化 ", " ChildFragment onStart()=" + program_id);
//        changePosition(PlayerState);
        super.onStart();
    }

    @Override
    public void onPause() {
        Log.e(" 初始化 ", " ChildFragment onPause()=" + program_id + " isVisible= " + isVisible()
                + " isResumed= " + isResumed());
        super.onPause();
        if (isResumed() && isVisible())
            changePosition(PlayerState.State.Pause);
        else
            changePosition(PlayerState.State.Stop);
    }

    @Override
    public void onResume() {
        Log.e(" 初始化 ", " ChildFragment onResume()=" + program_id + " isVisible= " + isVisible());
//        fragmentBanner.startPlayNow();
        changePosition(PlayerState.State.Start);
        super.onResume();
    }

    @Override
    public void onStop() {
        super.onStop();
//        fragmentBanner.stopPlayNow();
//        fragmentBanner.resetPlayNow();
        changePosition(PlayerState.State.Stop);
        Log.e(" 初始化 ", " ChildFragment onStop()=" + program_id);
    }

    @Override
    public void onDestroy() {
        Log.e(" 初始化 ", " ChildFragment onDestroy()=" + program_id);
//        changePosition(PlayerState.State.Release);
        super.onDestroy();
    }

    private void changePosition(int state) {
        PlayerState playerState = new PlayerState();
        playerState.setProgram_list_id(program_list_id);
        playerState.setProgram_id(program_id);
        playerState.setState(state);
        Log.e("发送指令  父类","节目单："+program_id +"  播放="+state);
        Rxbus.getInStance().post(Field_Banner_Player.Fragment_Banner_Player, playerState);
    }
}